package com.example.padlock.data.task;

import java.net.ConnectException;

import org.json.JSONException;

import com.example.padlock.data.api.HttpApi.OnFetched;
import com.example.padlock.data.common.asyntask.BaseAsyncTask;
import com.example.padlock.data.entity.AdResultEntity;
import com.example.padlock.data.impl.GetAdImpl;

public class GetAdTask extends BaseAsyncTask<String, Integer, Integer> {

	private AdResultEntity mResult;
	private OnFetched mOnFetched;

	public GetAdTask(OnFetched mOnFetched) {
		this.mOnFetched = mOnFetched;
	}

	@Override
	protected Integer doInBackground(String... params) {
		GetAdImpl impl=new GetAdImpl();
		try {
			AdResultEntity result = impl.getSingleByPost(params);
			this.mResult = result;
		} catch (ConnectException e) {
			e.printStackTrace();
			msg = "连接异常，错误码：" + e.getMessage();
			return FAIL;
		} catch (JSONException e) {
			e.printStackTrace();
			msg = "Json解析异常！错误码：" + e.getMessage();
			return FAIL;
		}
		return SUCCESS;
	}

	@Override
	protected void onPostExecute(Integer result) {
		switch (result) {
		case SUCCESS:
			if(null != mResult){
				mOnFetched.onAdFetched(mResult);
			}else{
				mOnFetched.onFail(msg);
			}
			break;

		case FAIL:
			mOnFetched.onFail(msg);
			break;
		}
	}

}
